package cn.melonlib.nars.account.comp.service;

import cn.hutool.core.bean.BeanUtil;
import cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService;
import cn.melonlib.cores.identification.comp.messenger.MessengerUtils;
import cn.melonlib.cores.identification.model.entity.Identification;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import cn.melonlib.cores.jpa.model.bean.impl.OffsetQueryImpl;
import cn.melonlib.cores.model.anno.*;
import cn.melonlib.nars.account.model.entity.AccountConfig;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

import static cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService.ROOT_METHOD.*;

@WorkerComment("账户信息")
@WorkerService(
        module = "account"
        ,model = @WorkerModel(
                modelType = AccountConfig.class
)
        ,ignores = {SAVE,REMOVE_BY_ID,REMOVE_BY_ID_LIST}
)
@Service
public class AccountConfigService extends WorkerBasicService<AccountConfig> {

    @Resource
    private MessengerUtils messengerUtils;

    @WorkerComment("创建账户")
    @WorkerStore(successTip = "创建成功",errorTip = "创建失败")
    public AccountConfig createAccount() throws Exception{
        Identification identification=getIdentificationMessenger().getIdentification();
        List<AccountConfig> accountConfigs=dao.<List<AccountConfig>,OffsetQueryImpl>search(new OffsetQueryImpl(AccountConfig.class, JsonMap.<JsonMap<Object>>createMap()
                .append("creatorId",JsonMap.createMap().append("eq",identification.getAccount())),0,1)).getResult();
        if(accountConfigs.isEmpty()){
            AccountConfig accountConfig=new AccountConfig();
            messengerUtils.setMessengers(accountConfig);
            return dao.save(accountConfig).getResult();
        }
        return accountConfigs.get(0);
    }

    @WorkerComment("获取当前账号")
    @WorkerQuery
    public AccountConfig currentAccount(){
        Identification identification=getIdentificationMessenger().getIdentification();
        List<AccountConfig> accountConfigs=dao.<List<AccountConfig>,OffsetQueryImpl>search(new OffsetQueryImpl(AccountConfig.class, JsonMap.<JsonMap<Object>>createMap()
                .append("creatorId",JsonMap.createMap().append("eq", BeanUtil.getProperty(identification,"id")))
                .append("creatorCode",JsonMap.createMap().append("eq",identification.getAccount()))
                ,0,1)).getResult();
        Assert.notEmpty(accountConfigs,"未找到账号，请先创建");
        return accountConfigs.get(0);
    }

    @WorkerComment("添加空间")
    @WorkerStore(successTip = "添加成功",errorTip = "添加失败")
    public AccountConfig addSpace(
            @WorkerParameter(comment = "账号ID",required = true) String id,
            @WorkerParameter(comment = "添加空间",required = true) Long addSpaceSize){
        AccountConfig accountConfig=dao.getEntity(AccountConfig.class,id);
        accountConfig.setSpace(accountConfig.getSpace()+addSpaceSize);
        return dao.save(accountConfig).getResult();
    }


}
